package com.example.demo.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.example.demo.feign.FeignClientServer;
import com.example.demo.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller


/**
 *
 * //@RestController 该页面有静态资源，所以不能使用RestController注解
 * #1. Controller, RestController的共同点
 * 都是用来表示Spring某个类的是否可以接收HTTP请求
 * #2. Controller, RestController的不同点
 *  @Controller：　　标识一个Spring类是Spring MVC controller处理器
 *  @RestController： 　　 @RestController是@Controller和@ResponseBody的结合体，两个标注合并起来的作用。
 * @Controller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。在方法上加@ResponseBody注解，也可以返回实体对象。
 *
 * @RestController类中的所有方法只能返回String、Object、Json等实体对象，不能跳转到模版页面。
 */
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private FeignClientServer feignClientServer;

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public ModelAndView indexDefault(){
        log.info("访问默认页面：/index");
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    @RequestMapping("/index")
    public ModelAndView index(){
        log.info("访问主页：/index");
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    @RequestMapping("/about")
    public String about(){
        log.info("访问页面：about");
        return "about";
    }

    @RequestMapping("/games")
    public String games(){
        log.info("访问页面：games");
        return "games";
    }

    @RequestMapping("/news")
    public String news(){
        log.info("访问页面：news");
        return "news";
    }

    @RequestMapping("/contact")
    public String contact(){
        log.info("访问页面：contact");
        return "contact";
    }

    @RequestMapping("/single")
    public String single(){
        log.info("访问页面：single");
        return "single";
    }

    //@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private String useLocalCache;
    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public String get() {
        //return useLocalCache;
        //return feignClientServer.get();
        return userService.getUserList().get(0).toString();
    }

    @RequestMapping(value = "/user")
    @ResponseBody
    public String user(){
        String result = feignClientServer.user();
        return result;
    }

    @NacosInjected
    private NamingService namingService;

    @RequestMapping(value = "/server", method = GET)
    @ResponseBody
    public List<Instance> get(@RequestParam String serviceName) throws NacosException {
        //namingService.registerInstance("demo", "127.0.0.1", 80);
        return namingService.getAllInstances(serviceName);
    }

    @ApiOperation(value = "打印测试")
    @GetMapping(value = "/printTest")
    public String printTest() {
        return userService.printTest();
    }

}
